<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
      <el-form-item label="公告标题" prop="like.title">
        <el-input v-model="searchForm.like.title" placeholder="请输入公告标题" clearable/>
      </el-form-item>
      <el-form-item label="操作人员" prop="like.create_by">
        <el-input v-model="searchForm.like.create_by" placeholder="请输入操作人员" clearable/>
      </el-form-item>
      <el-form-item label="类型" prop="where.type">
        <el-select v-model="searchForm.where.type" placeholder="公告类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPerm="['system:notice:add']">新增</el-button>
      <el-button type="success" plain icon="el-icon-edit" @click="handleEdit" v-hasPerm="['system:notice:edit']" :disabled="ids.length !== 1">修改</el-button>
      <el-button type="danger" plain icon="el-icon-delete" @click="handleDelete" v-hasPerm="['system:notice:delete']" :disabled="!ids.length">删除</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList" />
    </div>

    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="60" />
      <el-table-column label="公告标题" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column label="公告类型" align="center" prop="type" width="100">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_notice_type" :value="row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template v-slot="{ row }">
          <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="create_by" width="100" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="100">
        <template v-slot="{ row }">
          {{ parseTime(row.create_time, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template v-slot="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" v-hasPerm="['system:notice:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerm="['system:notice:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="getList"/>
    <NoticeForm v-model="formShow" :id="formId" v-if="formShow" @submit="getList"/>
  </div>
</template>

<script>
import { listNoticeApi, delNoticeApi } from '@/api/system/notice'
import NoticeForm from  './form.vue'

export default {
  name: "Notice",
  dicts: ['sys_normal_disable', 'sys_notice_type'],
  components: { NoticeForm },
  data() {
    return {
      loading: true,
      showSearch: true,
      ids: [],
      formId: null,
      formShow: false,
      listData: [],
      total: 0,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        like: { title: undefined, create_by: undefined },
        where: { status: undefined, type: undefined },
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listNoticeApi(this.searchForm).then(res => {
        this.listData = res.list;
        this.total = res.total;
        this.loading = false;
      });
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList();
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search();
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    handleAdd() {
      this.formId = null
      this.formShow = true
    },
    handleEdit(row) {
      this.formId = row?.id || (this.ids.length ? this.ids[0] : '')
      if (!this.formId) return this.$modal.msgError('参数缺失')
      this.formShow = true
    },
    handleDelete(row) {
      const noticeIds = row.id || this.ids.join(',')
      if (!noticeIds) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(() => {
        return delNoticeApi(noticeIds)
      }).then((res) => {
        this.getList()
        this.$modal.msgSuccess(res.msg || '删除成功')
      }).catch(() => {})
    }
  }
};
</script>
